package com.soap.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soap.commons.ReturnCommons;
import com.soap.entity.dto.PersonDto;
import com.soap.entity.po.Person;
import com.soap.manager.PersonManager;
import com.soap.service.PersonService;
import com.soap.utils.AuthUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by ZhangPY on 2020/6/7
 * Belong Organization OVERUN-9299
 * overun9299@163.com
 * Explain:
 */
@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonManager personManager;
    @Autowired
    private CreateLog createLog;

    @Override
    public String getPersonListForExport(PersonDto personDto) {
        /** 定义查询 */
        QueryWrapper<Person> wrapper = new QueryWrapper<>();
        Page<Person> page = new Page<>(personDto.getPage(), personDto.getLimit());

        if (personDto.getDId() != null) {
            wrapper.eq("d_id" , personDto.getDId());
        }

        if (personDto.getId() != null) {
            wrapper.eq("id" , personDto.getId());
        }

        if (personDto.getSex() != null) {
            wrapper.eq("sex" , personDto.getSex());
        }
        IPage<Person> personIPage = personManager.page(page, wrapper);

        return JSONObject.toJSONString(personIPage.getRecords());
    }


    @Override
    public String getPersonListByPage(PersonDto personDto) {
        /** 定义查询 */
        QueryWrapper<Person> wrapper = new QueryWrapper<>();
        Page<Person> page = new Page<>(personDto.getPage(), personDto.getLimit());

        if (personDto.getDId() != null) {
            wrapper.eq("d_id" , personDto.getDId());
        }

        if (personDto.getId() != null) {
            wrapper.eq("id" , personDto.getId());
        }

        if (personDto.getSex() != null) {
            wrapper.eq("sex" , personDto.getSex());
        }
        IPage<Person> personIPage = personManager.page(page, wrapper);

        return ReturnCommons.getSuccess(personIPage);
    }

    @Override
    public String getPersonById(Long personId) {
        if (personId == null) {
            return ReturnCommons.getFail("personId不能为空");
        }

        Person person = personManager.getById(personId);

        if (person == null) {
            return ReturnCommons.getFail("person不存在");
        }

        return ReturnCommons.getSuccess(person);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String testAdd(Person personDto) {

        Person person = new Person();
        BeanUtils.copyProperties(personDto , person);
        personManager.save(person);

        try {
            int i = 1/0;
        } catch (Exception e) {

            AuthUtils.test();
            throw new RuntimeException();
        }


        return null;
    }

    public void test() {
        Person person2 = new Person();
        person2.setAddress("123");
        person2.setFName("iii");
        person2.setSex(true);
        person2.setDId(2l);
        person2.setLName("jjj");
        person2.setDescribes("123123");
        person2.setAge(3);
        createLog.createdLog(person2);
    }
}
